package com.wanho.java159.cb.controller;

import com.github.pagehelper.PageInfo;
import com.wanho.java159.common.result.R;
import com.wanho.java159.cb.po.CarInfo;
import com.wanho.java159.cb.service.CarInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
* CarInfo 控制层
* @author ChaoFei.Zhang
* @date   2021-07-10
*/
@Api(tags = "管理CarInfo")
@CrossOrigin
@RestController
@RequestMapping("/admin/carInfo")
public class CarInfoController {
    /**CarInfo业务层对象 */
    @Resource
    private CarInfoService carInfoService;

    /**
    * 查询所有CarInfo
    * @return 封装的:list 所有的CarInfo数据
    */
    @ApiOperation("列表查询CarInfo")
    @GetMapping("/list")
    public R listAll(){
        return R.ok().data("list",carInfoService.list());
    }


    /**
    * 根据id删除CarInfo
    * @param id CarInfo的主键
    * @return ok 删除成功  error 删除失败
    */
    @ApiOperation(value = "根据id删除CarInfo", notes = "逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(@PathVariable("id") @ApiParam(value = "数据id", required = true, example = "100") Long id){
        boolean remove = carInfoService.remove(id);
        if (remove){
        return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    /**
    * 新增一条CarInfo记录
    * @param carInfo 封装新增的CarInfo数据
    * @return ok 新增成功  error 新增失败
    */
    @ApiOperation(value = "新增CarInfo", notes = "新增CarInfo")
    @PostMapping("/save")
    public R save(@ApiParam(value = "封装新增CarInfo对象", required = true) @RequestBody CarInfo carInfo){
        boolean remove = carInfoService.save(carInfo) ;
        if (remove){
            return R.ok().message("新增成功");
        }
        return R.error().message("新增失败");
    }

    /**
    * 根据id查询一条CarInfo记录
    * @param id CarInfo的主键
    * @return 封装 record 存储 CarInfo对象
    */
    @ApiOperation(value = "根据id查询CarInfo", notes = "根据id查询CarInfo")
    @GetMapping("/get/{id}")
    public R get(@PathVariable("id") @ApiParam(value = "数据id", required = true, example = "100") Long id){
        return R.ok().data("record",carInfoService.get(id)) ;
    }

    /**
    * 修改一条CarInfo记录
    * @param carInfo 封装CarInfo对象
    * @return ok 修改成功  error 修改失败
    */
    @ApiOperation(value = "修改CarInfo", notes = "修改CarInfo")
    @PutMapping("/update")
    public R update(@ApiParam(value = "封装修改CarInfo对象", required = true) @RequestBody CarInfo carInfo){
        boolean remove = carInfoService.update(carInfo) ;
        if (remove){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    /**
    * 分页条件查询CarInfo记录
    * @param page 页码
    * @param limit 分页跨度
    * @param keyword 查询关键字
    * @return pageModel 封装分页对象
    */
    @ApiOperation("获取CarInfo分页列表")
    @GetMapping(value = "/list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(value = "查询对象", required = false)
                    String keyword) throws UnsupportedEncodingException {
        System.out.println("搜索关键字=============1111："+keyword);
        PageInfo<CarInfo> pageModel =  carInfoService.listPage(page,limit, keyword);
        return R.ok().data("pageModel", pageModel);
    }
    /*@ApiOperation("获取CarInfo分页列表")
    @GetMapping(value = "/list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(value = "查询对象", required = false)
                    CarInfo carInfo) {
        PageInfo<CarInfo> pageModel =  carInfoService.listPage(page,limit, carInfo);
        return R.ok().data("pageModel", pageModel);
    }*/

    @ApiOperation("上传图片到OSS中")
    @PostMapping(value = "/uploadPic")
    public R uploadPic(MultipartFile file) {
        String picLoction = carInfoService.uploadPic(file);
        return R.ok().data("icon_url", picLoction);
    }
}
